package com.example.easybuy.controller;

import com.example.easybuy.service.AddressService;
import com.example.easybuy.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    @RequestMapping("/findAddress")
    public AjaxResult findAddress(int id) {
        return AjaxResult.success().add("address", addressService.findAddress(id));
    }

    @RequestMapping("/selectAll")
    public AjaxResult selectAll(@RequestParam Map<String,Object> map) {
        return AjaxResult.success().add("list", addressService.selectAll(map));
    }

    @RequestMapping("/addAddress")
    public AjaxResult addAddress(@RequestParam Map<String,Object> map) {
        return AjaxResult.success().add("isAdd", addressService.addAddress(map));
    }

    @RequestMapping("/updateAddress")
    public AjaxResult updateAddress(@RequestParam Map<String,Object> map) {
        if (map.get("isDefault").toString().equals("1")){
            addressService.updateIsDefault(Integer.parseInt(map.get("id").toString()));
        }
        return AjaxResult.success().add("isUpdate", addressService.update(map));
    }
    @RequestMapping("/delAddress")
    public AjaxResult delAddress(@RequestParam Map<String,Object> map) {
        return AjaxResult.success().add("isDel", addressService.delAddress(map));
    }

    @RequestMapping("/setDefault")
    public AjaxResult setDefault(int id) {
        return AjaxResult.success().add("isSet", addressService.updateIsDefault(id));
    }

}
